package com.shop.cereshop.admin.service.im.impl;

import com.github.pagehelper.PageHelper;
import com.shop.cereshop.admin.dao.imreport.ImReportMapper;
import com.shop.cereshop.admin.page.im.ImReportVO;
import com.shop.cereshop.admin.page.report.ReportRecordPageVO;
import com.shop.cereshop.admin.param.imreport.ImReportParam;
import com.shop.cereshop.admin.service.buyer.UserStopSpeeckService;
import com.shop.cereshop.admin.service.im.ImReportService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.im.tx.req.user.SetGlobalBanReq;
import com.shop.cereshop.commons.domain.im.tx.resp.ImBaseResp;
import com.shop.cereshop.commons.domain.imReport.CereImReport;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.service.im.ImUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

import java.util.Date;
import java.util.List;

@Service
public class ImReportServiceImpl implements ImReportService {

    @Autowired
    private ImReportMapper imReportMapper;
    @Autowired
    private    ImUserService imUserService;
    @Autowired
    private UserStopSpeeckService userStopSpeeckService;
    @Override
    public Page<ImReportVO> getReportPage(ImReportParam param) throws CoBusinessException {
        PageHelper.startPage(param.getPage(), param.getPageSize());

        List<ImReportVO> list = imReportMapper.findByCondition(param);
        PageInfo<ImReportVO> pageInfo = new PageInfo<>(list);

        return new Page<>(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public CereImReport getReportById(Long reportId) throws CoBusinessException {
        return imReportMapper.selectByPrimaryKey(reportId);
    }

    @Override
    public Boolean updateReport(CereImReport param) throws CoBusinessException {
        param.setCreatedTime(new Date());
        int rows = imReportMapper.updateByPrimaryKeySelective(param);
        return rows > 0;
    }

    @Override
    public Result<Void> auditReport(ImReportParam param) throws CoBusinessException {
        CereImReport report = new CereImReport();
        report.setReportId(param.getReportId());
        report.setReportStatus(param.getReportStatus());
        report.setReportResult(param.getReportResult());
        report.setReportHandleTime(new Date());
        report.setHandleType(param.getHandleType());
        report.setDuration(param.getDuration());

        //如果handleType 等于2和3
        if (param.getHandleType().equals("2")||param.getHandleType().equals("3")){
            SetGlobalBanReq setGlobalBanReq  = new SetGlobalBanReq();
            setGlobalBanReq.setSetAccount(param.getReportTargetId().toString());
            Long l = convertToSeconds(param.getDuration());
            if(param.getHandleType().equals("2")){
                setGlobalBanReq.setC2CmsgNospeakingTime(l);
            }else{
                setGlobalBanReq.setGroupmsgNospeakingTime(l);
            }
            imUserService.setGlobalBan(setGlobalBanReq);
        }

        int rows = imReportMapper.updateByPrimaryKeySelective(report);
        if (rows > 0){
            return new Result<>(CoReturnFormat.SUCCESS);
        }else {
            return new Result<>(CoReturnFormat.SYNC_ERROR);
        }
    }

    private Long convertToSeconds(Integer duration) {
        switch (duration) {
            case 1:
                return 60L; // 1分钟
            case 2:
                return 3600L; // 1小时
            case 3:
                return 604800L; // 1周
            case 4:
                return 2592000L; // 1月（30天）
            case 5:
                return 31536000L; // 1年（365天）
            default:
                return 0L; // 默认或无效值
        }
    }

}
